package org.chy.service.impl;

import java.util.List;

import org.chy.config.CustomUserDetails;
import org.chy.entity.User;
import org.chy.repository.UserRepository;
import org.chy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author  chenghy
 * @date    2018年01月15日 14时11分38秒
 * @Description 发上等愿,结中等缘,享下等福;向高处立,就平处坐,从宽处行.
 * @Package org.chy.service.impl
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	
	private @Autowired UserRepository userRepository;

	@Override
	public List<User> findAllUsers() {
		return userRepository.findAll();
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userRepository.selectUserByUsername(username);
		if (user == null) {
			 throw new UsernameNotFoundException("Could not find the user '" + username + "'");
		}
		// Not involve authorities, so pass null to authorities
        return new CustomUserDetails(user, true, true, true, true, null);
	}

	@Override
	public User selectUserByUsername(String username) {
		return userRepository.selectUserByUsername(username);
	}
}
